﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace Aim2 {
    public abstract class AnimatedObject {
        public float Scale { get; set; }
        public float Rotation { get; set; }
        public float Depth { get; set; }

        public float CollisionDamage { get; set; }

        public bool Inactive { get; set; }

        protected float Health { get; set; }

        protected IObjectContainer owner;

        protected int frames;
        protected int framesPerSec;
        protected float speed;
        private float inactiveTimer;
        public AnimatedSprite animatedSprite;
        
        protected Vector2 position;
        public Vector2 Position { get { return position; } set { position = value; } }
        
        public Rectangle HitBox {
            get {
                return new Rectangle(   (int) ((Position.X - ((animatedSprite.SourceRect.Width * Scale) / 2f))),
                                        (int) ((Position.Y - ((animatedSprite.SourceRect.Height * Scale) / 2f))),
                                        (int) (animatedSprite.SourceRect.Width * Scale),
                                        (int) (animatedSprite.SourceRect.Height * Scale));
            }
        }

        protected Tuple<int, int> layout;
        protected int width, height;

        static bool showHitbox = false;

        public abstract void Load(ContentManager Content, Viewport viewport);
        public virtual void Update(float elapsed) {
            if(Microsoft.Xna.Framework.Input.Keyboard.GetState().IsKeyDown(Microsoft.Xna.Framework.Input.Keys.H))
                showHitbox = !showHitbox;
            float xMargin, yMargin;

            xMargin = Game.Instance.GraphicsDevice.Viewport.Width * 0.1f;
            yMargin = Game.Instance.GraphicsDevice.Viewport.Height * 0.1f;
            if (this.Position.X > Game.Instance.GraphicsDevice.Viewport.Width + xMargin
                || this.Position.X < 0 - xMargin
                || this.Position.Y > Game.Instance.GraphicsDevice.Viewport.Height + yMargin
                || this.Position.Y < 0 - yMargin) 
                owner.AddToRemovalList(this);
            

            if (Inactive) {
                inactiveTimer += elapsed;
                if (inactiveTimer >= CollisionManager.TimeToBecomeActive)
                    this.Inactive = false;
            }
        }
        public virtual void Draw(SpriteBatch spriteBatch) {
            if(showHitbox)
                spriteBatch.Draw(Aim2.Screen.ScreenManager.blankTexture, HitBox, Color.White);
        }
        protected Side side;
        public abstract Side Side();

        public virtual void TakeDamage(float f) {
            Health -= f;
            Inactive = true;
            inactiveTimer = 0f;
            if (Health <= 0)
                owner.AddToRemovalList(this);
        }
    }
}
